package tencent_practice;

import structure.ListNode;

/**
 * 反转链表
 */
public class Tencent_206_reverseList {
    /**
     * 迭代的方式
     * @param head
     * @return
     */
    public ListNode reverseList(ListNode head) {
        if (head == null || head.next == null) {
            return head;
        }
        ListNode temp = new ListNode();
        ListNode curr = null;
        while (head.next != null) {
            temp = head.next;
            head.next = curr;
            curr = head;
            head = temp;
        }
        head.next = curr;
        return head;
    }

    /**
     * 递归的方式
     * @param head
     * @param noUse
     * @return
     */
    public ListNode reverseList(ListNode head, boolean noUse) {
        if (head == null || head.next == null) {
            return head;
        }
        ListNode temp  = reverseList(head.next, false);
        head.next.next = head;
        head.next = null;
        return temp;
    }
}
